package com.mars.web.common.support;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mars.web.common.pojo.ResponseResult;

/**
 * Spring MVC 控制器超类
 * 
 * <p>标题: SupperController</p>
 * <p>描述: </p>
 * <p>公司: www.yhaoquan.com</p> 
 * @作者： 杨浩泉
 * @日期：2016年11月18日
 * @版本：1.0
 */
public class SupperController {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected HttpServletRequest request;

	@Autowired
	protected HttpServletResponse response;

	/** 视图前缀 */
	protected String viewPrefix;

	public SupperController() {
		setViewPrefix(defaultViewPrefix());
	}
	
	/**
	 * 响应客户端数据
	 * @param status 响应状态（成功：true，失败：false）
	 * @param code 响应代码
	 * @param message 响应信息
	 * @param data 响应数据
	 * @param attrs 响应属性数据
	 * @return
	 */
	protected ResponseEntity<ResponseResult> setResponseResult(Boolean status, Integer code, String message, Object data, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(status, code, message, data, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(Integer code, String message, Object data, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(null, code, message, data, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(Integer code, String message, Object data) {
		return ResponseEntity.ok(new ResponseResult(null, code, message, data, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(Integer code, String message, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(null, code, message, null, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(Integer code, String message) {
		return ResponseEntity.ok(new ResponseResult(null, code, message, null, null)) ;
	}
	
	protected ResponseEntity<ResponseResult> setResponseResult(Boolean status, String message, Object data, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(status, null, message, data, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(Boolean status, String message, Object data) {
		return ResponseEntity.ok(new ResponseResult(status, null, message, data, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(Boolean status, String message, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(status, null, message, null, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(Boolean status, String message) {
		return ResponseEntity.ok(new ResponseResult(status, null, message, null, null)) ;
	}
	
	protected ResponseEntity<ResponseResult> setResponseResult(Boolean status, Integer code, String message) {
		return ResponseEntity.ok(new ResponseResult(status, code, message, null, null)) ;
	}
	
	protected ResponseEntity<ResponseResult> setResponseResult(ResponseResult responseResult) {
		return ResponseEntity.ok(responseResult) ;
	}
	
	
	protected ResponseEntity<ResponseResult> setResponseResultOk() {
		return ResponseEntity.ok(new ResponseResult(true, 200, "ok", null, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultFailed() {
		return ResponseEntity.ok(new ResponseResult(false, 400, "操作失败", null, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultError() {
		return ResponseEntity.ok(new ResponseResult(false, 500, "服务器内部错误", null, null)) ;
	}
	
	protected ResponseEntity<ResponseResult> setResponseResultOk(String message, Object data, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(true, 200, message, data, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultOk(String message, Object data) {
		return ResponseEntity.ok(new ResponseResult(true, 200, message, data, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultOk(String message, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(true, 200, message, null, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultOk(Object data, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(true, 200, "ok", data, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultOk(Object data) {
		return ResponseEntity.ok(new ResponseResult(true, 200, "ok", data, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultOk(String message) {
		return ResponseEntity.ok(new ResponseResult(true, 200, message, null, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultOk(Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(true, 200, "ok", null, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultFailed(String message, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(false, 400, message, null, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultFailed(String message) {
		return ResponseEntity.ok(new ResponseResult(false, 400, message, null, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultError(String message, Map<String, Object> attrs) {
		return ResponseEntity.ok(new ResponseResult(false, 500, message, null, attrs)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultError(String message) {
		return ResponseEntity.ok(new ResponseResult(false, 500, message, null, null)) ;
	}
	
	protected ResponseEntity<ResponseResult> setResponseResultAddOk() {
		return ResponseEntity.ok(new ResponseResult(true, 200, "添加成功", null, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultAddFailed() {
		return ResponseEntity.ok(new ResponseResult(false, 400, "添加失败", null, null)) ;
	}
	
	protected ResponseEntity<ResponseResult> setResponseResultDeleteOk() {
		return ResponseEntity.ok(new ResponseResult(true, 200, "删除成功", null, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultDeleteFailed() {
		return ResponseEntity.ok(new ResponseResult(false, 400, "删除失败", null, null)) ;
	}
	
	protected ResponseEntity<ResponseResult> setResponseResultUpdateOk() {
		return ResponseEntity.ok(new ResponseResult(true, 200, "修改成功", null, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultUpdateFailed() {
		return ResponseEntity.ok(new ResponseResult(false, 400, "修改失败", null, null)) ;
	}
	
	protected ResponseEntity<ResponseResult> setResponseResultGetOk() {
		return ResponseEntity.ok(new ResponseResult(true, 200, "加载对象成功", null, null)) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResultGetFailed() {
		return ResponseEntity.ok(new ResponseResult(false, 400, "加载对象失败", null, null)) ;
	}
	
	
	/**
	 * 响应客户端数据
	 * @param httpStatus 响应客户端Http状态码
	 * @param status 响应状态（成功：true，失败：false）
	 * @param code 响应代码
	 * @param message 响应信息
	 * @param data 响应数据
	 * @param attrs 响应属性数据
	 * @return
	 */
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, Boolean status, Integer code, String message, Object data, Map<String, Object> attrs) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(status, code, message, data, attrs), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, Integer code, String message, Object data) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(null, code, message, data, null), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, Integer code, String message, Map<String, Object> attrs) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(null, code, message, null, attrs), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, Boolean status, String message, Object data) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(status, null, message, data, null), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, Boolean status, String message, Map<String, Object> attrs) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(status, null, message, null, attrs), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, Boolean status, Integer code, String message) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(status, code, message, null, null), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, Integer code, String message) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(null, code, message, null, null), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, Boolean status, String message) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(status, null, message, null, null), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, String message) {
		return new ResponseEntity<ResponseResult>(new ResponseResult(null, null, message, null, null), httpStatus) ;
	}
	protected ResponseEntity<ResponseResult> setResponseResult(HttpStatus httpStatus, ResponseResult responseResult) {
		return new ResponseEntity<ResponseResult>(responseResult, httpStatus) ;
	}
	
	
	/**
	 * 获取Controller注解上的路径
	 * 
	 * @return
	 */
	protected String defaultViewPrefix() {
		String currentViewPrefix = "";
		RequestMapping requestMapping = AnnotationUtils.findAnnotation(getClass(), RequestMapping.class);
		if (requestMapping != null && requestMapping.value().length > 0) {
			currentViewPrefix = requestMapping.value()[0];
		}
		return currentViewPrefix;
	}

	/**
	 * 设置视图名称
	 * 
	 * @param suffixName
	 * @return
	 */
	public String viewName(String suffixName) {
		if (!suffixName.startsWith("/")) {
			suffixName = "/" + suffixName;
		}
		return getViewPrefix() + suffixName;
	}

	/**
	 * 设置视图前缀
	 * 
	 * @param viewPrefix
	 */
	public void setViewPrefix(String viewPrefix) {
		if (viewPrefix.startsWith("/")) {
			viewPrefix = viewPrefix.substring(1);
		}
		this.viewPrefix = viewPrefix;
	}

	public String getViewPrefix() {
		return viewPrefix;
	}

}
